package com.jl.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import com.jl.resolver.ContractRequestResponseBodyMethodProcessor;
import com.jl.util.ClassUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * 配置加载
 */
@Configuration
@EnableKnife4j
@AllArgsConstructor
@EnableSwagger2WebMvc
public class ConciseMvcConfig {

    private RequestMappingHandlerAdapter adapter;

    private WebMvcConfigurationSupport webMvcConfigurationSupport;

    private ApplicationContext applicationContext;

    /**
     * 调整Handlers顺序
     */
    @PostConstruct
    public void responseBodyReplace() {
        List<HttpMessageConverter<?>> messageConverters = ClassUtil.invokeNoParameterMethod(WebMvcConfigurationSupport.class, webMvcConfigurationSupport, "getMessageConverters");
        // 把自定义Handlers放在第一位
        List<HandlerMethodReturnValueHandler> handlers = new ArrayList<>(adapter.getReturnValueHandlers());
        handlers.set(0, new ContractRequestResponseBodyMethodProcessor(messageConverters));
        // 重新设置SpringMVC的ReturnValueHandlers
        adapter.setReturnValueHandlers(handlers);
    }

    /**
     * 加载swagger
     *
     * @return
     */
    @Bean
    public Docket docket() {
        OpenApiExtensionResolver openApiExtensionResolver = null;
        try {
            openApiExtensionResolver = applicationContext.getBean(OpenApiExtensionResolver.class);
        } catch (Exception e) {
        }
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("接口文档")
                        .description("接口文档")
                        .build()
                )
                .groupName("全部")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build();
        if (openApiExtensionResolver != null) {
            docket.extensions(openApiExtensionResolver.buildExtensions("全部"));
        }
        return docket;
    }
}
